import os

import numpy as np


def loadDatadet(infile, k):
    f = open(infile, 'r')
    sourceInLine = f.readlines()
    dataset = []
    for line in sourceInLine:
        temp1 = line.strip('\n')
        temp2 = temp1.split('\t')  # 以tab为分割 我们应该是以空格分割？
        dataset.append(temp2)
    for i in range(0, len(dataset)):
        for j in range(k):
            dataset[i].append(float(dataset[i][j]))
        del (dataset[i][0:k])
    return dataset


Count_file = 0
path = "C:/Users/admin/PycharmProjects/pythonProject2/Traces_TimeXY_30sec_txt/NewYork"  # 文件夹目录
files = os.listdir(path)  # 得到文件夹下的所有文件名称
txts = []
for file in files:  # 遍历文件夹
    position = path + '\\' + file  # 构造绝对路径，"\\"，其中一个'\'为转义符
    data = loadDatadet(position, 3)
    txts.append(data)

maxnumx = 0.
minnumx = 100000.
maxnumy = 0.
minnumy = 100000.
Time = np.zeros(1600)
for Count_file in range(np.array(txts).shape[0]):
    print((np.array(txts[Count_file])).shape[0])
    for i in range((np.array(txts[Count_file])).shape[0]):
        normalizex = float(txts[Count_file][i][2])
        if maxnumx < normalizex:
            maxnumx = normalizex
        if minnumx > normalizex:
            minnumx = normalizex
        normalizey = float(txts[Count_file][i][3])
        if maxnumy < normalizey:
            maxnumy = normalizey
        if minnumy > normalizey:
            minnumy = normalizey
userx = np.zeros((39, 2721))
usery = np.zeros((39, 2721))
Time = np.zeros((39, 2721))
for Count_file in range(np.array(txts).shape[0]):
    for i in range((np.array(txts[Count_file])).shape[0]):
        userx[Count_file][i] = (txts[Count_file][i][2] - minnumx) * 600 / (maxnumx - minnumx)
        usery[Count_file][i] = (txts[Count_file][i][3] - minnumy) * 400 / (maxnumy - minnumy)
        # print(userx[Count_file][i])
        # print(usery[Count_file][i])
        Time[Count_file][i] = txts[Count_file][i][1]
print(minnumx)
print(maxnumx)
print(txts[0][0][2])
print(userx[1][1])
print(np.array(txts).shape[0])

m = np.zeros((2, 2071))

print(m)
# np.array(txts).shape[0]
# print(minnumy)
# print(maxnumy)
Bwc = 2e6  # 2Mhz
A = 4.11  # 天线增益
f = 9.15e8  # 915Mhz
P = 0.25  # W 用户设备天线的发射功率
sigma = 1e-10
PI = 3.1415926
Din = 2.7e5  # 270KB
Ptask = 1.08e6  # 1080KB
Ci = 6.3e7  # 63MB/s
Kuser = np.zeros(50000)
Kaverage = 0
B = 1e9 / 8  # 1Gbps=1/8 GB
Dmig = 0
Last_action = 0
C = np.zeros(35)
tau = 5  # τ
tau_ = 15  # τ'
Time_ = 0
d = 3e2
h = A * ((3e8 / (4 * PI * f * d)) ** 2)  # 衰落信道
C = Bwc * np.log2(1 + P * h / (sigma ** 2))  # 香农容量
Trans = Din / C  # The transmission time can be computed if we divide data size by communication capacity
Tcomp = Ptask / Ci  # Computation latency considers servers’ computing ability Ci,resources R and task loading Ptask
Tmig = Ptask / (B / 2)  # Migration latency is determined by VM size and bandwidth B of the internet.  Each VM runs a
# SSD [15] model to detect objects.

Timeall = Tcomp + Trans + Tmig

print("d = ", d)
print("h =", h)
print("C = ", C)
print("Trans = ", Trans * 1000, "ms")
print("Tcomp = ", Tcomp * 1000, "ms")
print("Tmig = ", Tmig * 1000, "ms")
print("Timeall = ", Timeall * 1000, "ms")

from numpy import array
from numpy import argmax
from tensorflow.keras.utils import to_categorical
# define example
data = [1, 3, 2, 0, 3, 2, 2, 1, 0, 1]
data = array(data)
print(data)
# one hot encode
encoded = to_categorical(data)
print(encoded)
# invert encoding
inverted = argmax(encoded[0])
print(inverted)